package pers.jing.android.helper.widget

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Factory
import androidx.compose.material.icons.rounded.FileOpen
import androidx.compose.material.icons.rounded.OpenWith
import androidx.compose.material.icons.rounded.UploadFile
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.unit.dp
import org.jetbrains.compose.resources.stringResource
import java.awt.FileDialog
import java.io.File
import java.io.FilenameFilter

@Composable
fun OutlinedFileField(
    title: String,
    window: ComposeWindow? = null,
    filePath: String = "",
    placeHolderText: String = "",
    ext: String? = null,
    modifier: Modifier,
    onSelect: (String) -> Unit
) {
    Row(
        modifier = Modifier.then(modifier),
        verticalAlignment = Alignment.CenterVertically
    ) {
        OutlinedTextField(
            value = filePath,
            onValueChange = {

            },
            readOnly = true,
            modifier = Modifier.weight(1.0f),
            placeholder = {
                Text(placeHolderText)
            },
            label = {
                Text(text = title)
            },
            maxLines = 1,
            trailingIcon = {
                TextButton(
                    onClick = {
                        val fileDialog = FileDialog(
                            window, title, FileDialog.LOAD // 打开模式（LOAD=选择文件，SAVE=保存文件）
                        ).apply {
                            directory = System.getProperty("user.home")
                            filenameFilter = MyFilenameFilter(true, ext)
                        }

                        // 2. 显示对话框（阻塞当前线程，直到用户操作完成）
                        fileDialog.isVisible = true

                        // 3. 用户操作完成后，处理结果（此时对话框已关闭）
                        if (fileDialog.directory.isNullOrEmpty()
                            || fileDialog.file.isNullOrEmpty()) {
                            return@TextButton
                        }

                        // 4. 更新状态（触发UI刷新）
                        //选中文件之后, file仅有文件名, 需要拼接目录
                        onSelect(fileDialog.directory + fileDialog.file)
                    }
                ) {
                    Text(
                        text = "选择"
                    )
                }
            }
        )
    }
}

class MyFilenameFilter(val isFile: Boolean, val ext: String? = null): FilenameFilter {

    override fun accept(dir: File?, name: String?): Boolean {
        if (dir == null || name == null) {
            return false
        }
        val file = File(dir, name)
        if (isFile) {
            if (file.isDirectory) {
                return false
            }
            if (ext.isNullOrEmpty()) {
                return true
            }
            return file.name.endsWith(ext)
        } else {
            return file.isDirectory
        }
    }

}